package com.shop.apigateway.config;

import java.util.Arrays;
import java.util.Collections;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 跨域
 *
 * @author denpeng
 * @create 2018-05-25 11:39
 **/
@Configuration
public class CorsConfig {

  @Bean
  public CorsFilter corsFilter() {
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration configuration = new CorsConfiguration();

    configuration.setAllowCredentials(true);//应许cooke跨域
    configuration.setAllowedOrigins(Collections.singletonList("*"));//应许什么ip来访问
    configuration.setAllowedHeaders(Collections.singletonList("*"));//应许请求头
    configuration.setAllowedMethods(Collections.singletonList("*"));//应许什么请求方式
    configuration.setMaxAge(300L);//多少时间内不再重复验证

    source.registerCorsConfiguration("/**", configuration);
    return new CorsFilter(source);
  }
}
